package com.rd.student.ruanjian.class3.gaochenxi.day0728;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Test {

   public static String[] NumberOne(int n){
      String str = "郑成祥、王博、孙文卓、郑睿娴、王高博、李春泉、张睿、王星凯、柏云耀、高艺铭、周子贺、杜航、高晨曦、张思韬、孙昊、方周、郭佳宝、姜万里、李宏鸣、薛志鑫、王海鑫、夏银银、吴俊豪、王向阳、徐铭泽、张世煊、才雨鑫、孙国伟、秦嘉林、王岩博、石睿、杜雨坤、黄奕鸣、金洪宇、李鑫、王宇芳、苏岩";
      String[] split = str.split("、");
      String[] another=new String[]{};
      Random random = new Random();
      for(int i=0;i<n;i++){
         //把它保存在一个String[]当中
         another[i]=split[random.nextInt(str.length())];
      }
      return another;
   }

   //二
   public static void NumberTwo(int n) throws IOException {
      Reader reader = null;
      Random random = new Random();
      try {
         File file = new File("all.txt");
         reader = new FileReader(file);
         char[] chars = new char[(int)file.length()];
         reader.read(chars);//读到内存当中
         String s = new String((chars));
         String[] split = s.split("、");
         System.out.println("===================");
         for(int i=0;i<n;i++){
            System.out.println(split[random.nextInt(split.length)]);
         }
         System.out.println("===================");
         reader.close();
      } catch (FileNotFoundException e) {
         throw new RuntimeException(e);
      } catch (IOException e) {
         throw new RuntimeException(e);
      } finally {
         try {
            if(reader!=null){
               reader.close();
            }
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
      }
   }

   //三
   public static void NumberThree(File one, File two) throws IOException {
      int n=0;
      Scanner scanner=new Scanner(System.in);
      System.out.println("请输入想要获取的同学的个数:");
      n=scanner.nextInt();
      File all = new File("all.txt");
      File random = new File("random.txt");
      if (!all.exists() | !random.exists()) {
         all.createNewFile();
         random.createNewFile();
      }
      //把all.txt所有的人名全部转移到random.txt
      FileReader is = new FileReader(all);
      FileWriter os = new FileWriter(random);
      char[] chars = new char[(int)all.length()];
      int len = 0;
      while ((len = is.read(chars)) != -1) {//长度等于读取的字节数组的长度，如果不等于-1的话
         os.write(chars, 0, len);//就会一直往news.txt文件中写，从这个数组的长度下标为0开始，一直打印到下标为len为止
      }
      os.close();
      is.close();
   }
}
